<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

class CprojectModelCategories extends JModelList {

    var $_db = null;

    protected function populateState($ordering = null, $direction = null) {

        $app = JFactory::getApplication('administrator');

        //Load State 

        $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
        $this->setState('filter.search', $search);

        $published = $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_published', '', 'string');
        $this->setState('filter.state', $published);

        $section = JRequest::getInt('c', $this->getUserStateFromRequest($this->context . '.filter.section', 'filter_section', ''), 'GET');
        $this->setState('filter.section', $section);
        parent::populateState('a.name', 'asc');
    }

    protected function getStoreId($id = '') {
        // Compile the store id.
        $id.= ':' . $this->getState('filter.search');
        $id.= ':' . $this->getState('filter.state');
        return parent::getStoreId($id);
    }

    function getListQuery() {
        $where = array();

        $where = $this->_buildContentWhere();
        $this->_db = JFactory::getDbo();
        $query = $this->_db->getQuery(true);
        $query = CprojectDBHelper::getCprojectCategories($sql = true, $where);
        return $query;
    }

    function _buildContentWhere() {
        $where = array();
        $this->_db = JFactory::getDbo();

        //Filter by published state 
        $published = $this->getState('filter.state');
        if (is_numeric($published)) {
            $where[] = 'a.published = ' . (int) $published;
        } else if ($published == '') {
            $where[] = 'a.published IN (0, 1)';
        }

        //Tim kiem theo ten
        $search = $this->getState('filter.search');
        if (!empty($search)) {
            if (stripos($search, 'id:') === 0) {
                $query->where('a.id = ' . (int) substr($search, 3));
            } else {
                $search =  $this->_db->Quote('%' .  $this->_db->getEscaped($search, true) . '%');
                $where[] = 'a.name LIKE ' . $search;
            }
        }
        // Filter on the campus.
        $section = $this->getState('filter.section');
        if ($section) {
            $where[] = 'a.section_id = ' . (int) $section;
        }
        return $where;
    }

}

?>
